package com.expertria.glex.view;

import java.util.Iterator;

import com.expertria.glex.event.Events.EventDispatcherHelper;
import com.expertria.glex.event.Events.IEventDispatcher;
import com.expertria.glex.event.Events.IEventListener;
import com.expertria.glex.view.layout.AbsoluteLayout;
import com.expertria.glex.view.layout.ILayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.uibinder.client.UiConstructor;
import com.google.gwt.user.client.ui.ComplexPanel;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.InsertPanel;
import com.google.gwt.user.client.ui.Widget;

public class Group extends StateView implements InsertPanel , IEventDispatcher , HasWidgets {

	protected ILayout layout ;
	protected EventDispatcherHelper eventHelper = 
		new EventDispatcherHelper();
	
	
protected ComplexPanel basePanel;
	
	public    Group()
	{
		this(new AbsoluteLayout());
	}
	
	public @UiConstructor Group(ILayout layout)
	{
		
		super();
		
		this.layout = layout;
		
		this.basePanel = this.layout.getLayoutBase();
		
		initWidget(this.basePanel);
		
		

		 // Inject the contents of the CSS file
		 DefaultResources.INSTANCE.css().ensureInjected();	
	}
	
	@Override
	public void onStateChange(String oldState, String newState) {
		 
		
	}
	
	public ILayout getLayout() {
		return layout;
	}
	
	 
	public void setLayout(ILayout layout) {
		this.layout = layout;			
	}
	@Override
	public void insert(Widget w, int beforeIndex) {
	 
		 
		add(w);
		
	}
	
	/**
	 * Calls addWidget
	 */
	public void add(Widget child)
	{
		this.addWidget(child);
	}
	
	public void addWidget(Widget child)
	{ 
		this.basePanel.add(child);
		this.layout.onAddChild(child);	
		//loop for everything
		this.layout.updateDisplayList();
	}

	 
	
	
	 public interface DefaultResources extends ClientBundle {

		 public static final DefaultResources INSTANCE =  GWT.create(DefaultResources.class);

		    /**
		     * The css file.
		     */
		    @Source("glex-core.css")
		    @CssResource.NotStrict
		    public CssResource css();

		  }
	
	
	
	public int getWidgetIndex(Widget child)
	{
		return this.basePanel.getWidgetIndex(child);
	}
	
	@Override
	public int getWidgetCount() {
	 
		return this.basePanel.getWidgetCount();
	}
	
	@Override
	public Widget getWidget(int index) {
		 
		return this.basePanel.getWidget(index);
	}
	 
	@Override
	public boolean remove(int index) {
		 
		return this.basePanel.remove(index);
	}
	
	public boolean remove(Widget w)
	{
		int i = this.basePanel.getWidgetIndex(w);
		
		return remove(i);
		 
	}
	
	@Override
	public void addEventListener(String event, IEventListener e) {
		
		eventHelper.addEventListener(event, e);
		
	}
	
	@Override
	public void dispatch(String eventName, Object object) {
		 eventHelper.dispatch(eventName, object);
		
	}
	
	@Override
	public void dispose() {
		eventHelper.dispose();
	}
	
	@Override
	public void clear() {
		this.basePanel.clear();
		
	}
	
	@Override
	public Iterator<Widget> iterator() {
		 
		return this.basePanel.iterator();
	}
	
	
}
